package com.juzi.wopu.query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressWarnings("all")
public abstract class BaseQuery {

	private int currentPage = 1;
	
	private int pageSize = 10;

	private StringBuilder countHql;
	
	private StringBuilder limitHql;
	
	private StringBuilder where;

	private List paramList;

	public BaseQuery(String domainNmae) {
		countHql = new StringBuilder("select count(o) from "+domainNmae +" o");
		limitHql = new StringBuilder("select o from "+ domainNmae+" o");
		where=new StringBuilder();
		paramList = new ArrayList();
	}
	
	protected void AddWhere(String whereHql,Object...objects){
		if(paramList.size()==0){			
			countHql.append(" where ").append(whereHql);
			limitHql.append(" where ").append(whereHql);
			where.append(" where ").append(whereHql);
		}else{
			countHql.append(" and ").append(whereHql);
			limitHql.append(" and ").append(whereHql);
			where.append(" and ").append(whereHql);
		}
		paramList.addAll(Arrays.asList(objects));
	}
		
	protected abstract void addWhere();

	private boolean flag=false;
	
	private void builderwhere(){
		if(!flag){
			addWhere();
			flag=true;
		}
	}

	public void setCountHql(StringBuilder countHql) {
		this.countHql = countHql;
	}

	public String getLimitHql() {
		builderwhere();
		return limitHql.toString();
	}

	public String getCountHql() {
		builderwhere();
		return countHql.toString();
	}

	public void setLimitHql(StringBuilder limitHql) {
		this.limitHql = limitHql;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List getParamList() {
		builderwhere();
		return paramList;
	}

	public void setParamList(List paramList) {
		this.paramList = paramList;
	}

	public String getWhere() {
		builderwhere();
		return where.toString();
	}

	public void setWhere(StringBuilder where) {
		this.where = where;
	}
}
